<?php
/**
 * User: nguyenpc
 * Date: 9/19/14
 * Time: 10:02 PM
 */

namespace Backend\Modules\Video\Controllers;
use Backend\Modules\Video\Forms\FormCreateNational as FormCreateNational;
use Backend\Modules\Video\Models\National as VideoNational;
use Phalcon\Paginator\Adapter\NativeArray as Paginator;
use Phalcon\Mvc\Model\Resultset;
use Library\Pagination\Pager as Pager;


class NationalController extends \BackendController 
{
    public function indexAction()
    {

        $currentPage = abs($this->request->getQuery('page', 'int', 1));
        $national = VideoNational::find();
        $data = array();
        foreach ($national as $item) {
            $data[] = $item;
        }

        if ($currentPage == 0) {
            $currentPage = 1;
        }
        $pager = new Pager(
            new Paginator(array(
                'data'  => $data,
                'limit' => 5,
                'page'  => $currentPage,
            )),
            array(
                // We will use Bootstrap framework styles
                'layoutClass' => 'Library\Pagination\Pager\Layout\Bootstrap',
                // Range window will be 5 pages
                'rangeLength' => 5,
                // Just a string with URL mask
                'urlMask'     => 'http://localhost/javhd/javhd-admin/video/national?page={%page_number}',
                // Or something like this
                // 'urlMask'     => sprintf(
                //     '%s?page={%%page_number}',
                //     $this->url->get(array(
                //         'for'        => 'index:posts',
                //         'controller' => 'index',
                //         'action'     => 'index'
                //     ))
                // ),
            )
        );

        $this->view->pager = $pager;
    } 
    public function createAction()
    {
        $form = new FormCreateNational();
        $national = new VideoNational();
        if ($this->request->isPost()) {
            if ($form->isValid($this->request->getPost()) != false){
                $national->assign(array(
                    'name' => $this->request->getPost('name'),
                    'short_name' => $this->request->getPost('short_name')
                ));
                if ($national->save()) {
                    return $this->dispatcher->forward(array(
                            'controller' => 'national',
                            'action' => 'index'
                    ));
                }else{
                    $this->flash->error($national->getMessages()); 
                    
                }
                
            }
        }

        $this->view->form = $form;
    }
    public function editAction($id){

        $national = VideoNational::findFirstById($id);
        if(!$national)
        {
            $this->flash->error("Arist was not found");
            return $this->dispatcher->forward(array(
                'action' => 'index'
            ));
        }
        $form = new FormCreateNational($national , array('edit' => true));

        if($this->request->getPost())
        {
            if($form->isValid($this->request->getPost()) != false)
            {
                $national->assign(array(
                    'name' => $this->request->getPost('name'),
                    'short_name' => $this->request->getPost('short_name')
                ));

                if($national->save())
                {
                    return $this->dispatcher->forward(array(
                        'action' => 'index'
                    ));
                }
                $this->flash->error($national->getMessages());
            }
        }
        $this->view->form = $form;
    }
    public function deleteAction($id)
    {
        $national = VideoNational::findFirstById($id);
        if(!$national)
        {
            $this->flash->error("Nation was not found");
            return $this->dispatcher->forward(array(
                'action' => 'index'
            ));
        }
        if(!$national->delete())
        {
            $this->flash->error($national->getMessages());
        }
        else
        {
            $this->flash->success("Nation was deleted");
        }

        return $this->dispatcher->forward(array(
            'action' => 'index'
        ));
    }
} 